﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using ProjManagSolution.pmMyWebService;

namespace ProjManagSolution
{
    public partial class EditTask : ChildWindow
    {
        // List of auto collection
        List<Phase> listPhase = new List<Phase>();
        List<MemberEmail> listEmail = new List<MemberEmail>();
        
        protected int projectID;
        
        public EditTask(List<phase> pl, List<user> ml)
        {
            InitializeComponent();

            // Add items into collection;
            AddCollection(pl, ml);
            // Bind datacontext
            autoboxPhase.DataContext = listPhase;
            autoboxAssignedMember.DataContext = listEmail;
        }

        protected bool Input_Validate()
        {
            if (autoboxAssignedMember.SelectedItem == null)
            {
                MessageBox.Show("Please select a member!");
                return false;
            }

            if (autoboxPhase.SelectedItem == null)
            {
                MessageBox.Show("Please select a phase!");
                return false;
            }

            if ((datepickStartDate.SelectedDate < DateTime.Now) ||
                 (datepickDeadLine.SelectedDate < DateTime.Now))
            {
                MessageBox.Show("Start date/Deadline cannot be ealier than today!");
                return false;
            }
            if (DateTime.Compare((DateTime)datepickDeadLine.SelectedDate, (DateTime)datepickStartDate.SelectedDate) < 0)
            {
                MessageBox.Show("Start date must be ealier than deadline!");
                return false;
            }


            if (txtboxTaskName.Text == "")
            {
                MessageBox.Show("Taskname cannot be empty!");
                return false;
            }
            return true;
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if(Input_Validate())
                this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        // Add items into collection;
        private void AddCollection(List<phase> pl, List<user> ml)
        {
            for (int i = 0; i < pl.Count; ++i)
            {
                listPhase.Add(new Phase() { 
                    Description = pl[i].phase_description,
                    EndDate = pl[i].phase_end_date,
                    ID = pl[i].phase_id.ToString(),
                    MilestoneID = pl[i].milestone_id.ToString(),
                    Name = pl[i].phase_name,
                    StartDate =pl[i].phase_start_date
                });
            }

            autoboxPhase.ItemsSource = listPhase;

            for (int i = 0; i < ml.Count; ++i)
            {
                listEmail.Add(new MemberEmail() { 
                    m_ID = ml[i].user_id.ToString(),
                    m_Email = ml[i].user_email
                });
            }

            autoboxAssignedMember.ItemsSource = listEmail;
        }
    }
}

